package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_ClassifySecondLVAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Discover_ClassifySecondGetListService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_ClassifySecondBookSuspenseEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discover_ClassifySecondBaseFragment extends Fragment implements Callback<Discover_ClassifySecondBookSuspenseEntity>, AbsListView.OnScrollListener {

    private ListView lv_base;
    private TextView tv_empty;
    private String fragmentFlag;
    private String fragmentName;

    private List<Discover_ClassifySecondBookSuspenseEntity.SuspenseListEntity> dataList;
    private Discover_ClassifySecondLVAdapter discover_classifySecondLVAdapter;

    private Retrofit retrofit;
    private int pageId = 1;
    private int pageSize = 20;
    private int status = 0;
    private String categoryId = "3";
    private boolean isScrollToBottom;

    public Discover_ClassifySecondBaseFragment() {
        // Required empty public constructor
    }

    public static Discover_ClassifySecondBaseFragment newInstance(String fragmentFlag, String fragmentName,String categoryId) {

        Bundle args = new Bundle();
        args.putString("fragmentFlag", fragmentFlag);
        args.putString("fragmentName", fragmentName);
        args.putString("categoryId",categoryId);

        Discover_ClassifySecondBaseFragment fragment = new Discover_ClassifySecondBaseFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_discover__classify_second_base, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lv_base = (ListView) view.findViewById(R.id.classify_second_fragment_listview);
        tv_empty = (TextView) view.findViewById(R.id.classify_second_fragment_text);
        lv_base.setEmptyView(tv_empty);

        fragmentName = getArguments().getString("fragmentName");
        fragmentFlag = getArguments().getString("fragmentFlag");
        categoryId = getArguments().getString("categoryId");
        Log.d("1620", "onViewCreated: "+categoryId);
        tv_empty.setText(fragmentName);

        dataList = new ArrayList<>();

        lv_base.setOnScrollListener(this);

        retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        initData();
        listViewBindAdapter();

    }

    public void downloadData(){

        Discover_ClassifySecondGetListService discover_classifySecondBookListService = retrofit.create(Discover_ClassifySecondGetListService.class);
        discover_classifySecondBookListService
                .showList(categoryId,fragmentName, pageId,pageSize,status)
                .enqueue(this);
    }
    public void listViewBindAdapter(){
        discover_classifySecondLVAdapter = new Discover_ClassifySecondLVAdapter(getContext(), dataList);
        lv_base.setAdapter(discover_classifySecondLVAdapter);
    }

    private void initData() {

        if (fragmentName.equals(fragmentFlag)) {
            downloadData();

//            lv_base.smoothScrollToPosition(lv_base.getCount()-21);
        }

    }

    @Override
    public void onResponse(Call<Discover_ClassifySecondBookSuspenseEntity> call, Response<Discover_ClassifySecondBookSuspenseEntity> response) {

        Discover_ClassifySecondBookSuspenseEntity body = response.body();
        dataList.addAll(body.getList());
        discover_classifySecondLVAdapter.notifyDataSetChanged();
//        if (fragmentName.equals(fragmentFlag)) {
//            listViewBindAdapter();
//        }
    }

    @Override
    public void onFailure(Call<Discover_ClassifySecondBookSuspenseEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int state) {
        if (isScrollToBottom && state == SCROLL_STATE_IDLE) {
            pageId = pageId + 1;
            initData();
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem + visibleItemCount == totalItemCount) {
            isScrollToBottom = true;
        } else {
            isScrollToBottom = false;
        }
    }
}
